package com.propertypoint.service.business;


import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.propertypoint.exception.FeedErrorException;
import com.propertypoint.presentation.model.Property;
import com.propertypoint.service.general.PropertyFeedProcessStratergy;
import com.propertypoint.util.PropertyFeedUtil;
import com.propertypoint.util.PropertyPointConstants;

public class STPropertyFeedStratergy implements PropertyFeedProcessStratergy {
	
	public static final String D01 = "Boat Quay,Raffles Place,Marina";
	public static final String D02 = "Chinatown,Tanjong Pagar";
	public static final String D03 = "Alexandra,Commonwealth";
	public static final String D04 = "Harbourfront,Telok Blangah";
	public static final String D05 = "Buona Vista,West Coast,Clementi";
	public static final String D06 = "City Hall,Clarke Quay";
	public static final String D07 = "Beach Road,Bugis,Rochor";
	public static final String D08 = "Farrer Park,Serangoon";
	public static final String D09 = "Orchard,River Valley";
	public static final String D10 = "Tanglin Holland";
	public static final String D11 = "Newton,Novena";
	public static final String D12 = "Balestier,Toa Payoh";
	public static final String D13 = "Macpherson,Potong Pasir";
	public static final String D14 = "Eunos,GeyLang,Paya Lebar";
	public static final String D15 = "East Coast,Marine Parade";
	public static final String D16 = "Bedok,Upper East Coast";
	public static final String D17 = "Changi Airport,Changi Village";
	public static final String D18 = "Pasir Ris/Tampines";
	public static final String D19 = "Hougang/Punggol,Sengkang";
	public static final String D20 = "Ang Mo Kio,Bishan,Thomson";
	public static final String D21 = "Clementi Park,Upper Bukit Timah";
	public static final String D22 = "Boon Lay,urong,Tuas";
	public static final String D23 = "Bukit Batok,Bukit Panjang";
	public static final String D24 = "Choa Chu Kang,Tengah";
	public static final String D25 = "Admiralty,Woodlands";
	public static final String D26 = "Mandai,Upper Thomson";
	public static final String D27 = "Sembawang,yishun";
	public static final String D28 = "Seletar,Yio Chu Kang";
	
	public static final String LOCATION = "district";
	public static final String RENT_SALE = "rent_sale";
	public static final String MAX_PRICE = "maxrentprice";
	public static final String MAX_BED = "maxbed";
	public static final String MIN_BED = "minbed";
 
	@Override
	public List<Property> processFeed(Map<String, String[]> parameterMap) throws FeedErrorException {
		List<Property> propertyList = new ArrayList<Property>();
		try {
			
			JsonObject jsonObject = new JsonObject();	
			String[] location =parameterMap
					.get(PropertyPointConstants.LOCATION);
			String[] maxBed = parameterMap.get(PropertyPointConstants.BEDROOMS);
			String[] propertytype = parameterMap
					.get(PropertyPointConstants.TYPE);
	


			parameterMap = new HashMap<String, String[]>();
			parameterMap.put("district", location);
			jsonObject = PropertyFeedUtil.getInstance().callFeed(parameterMap, PropertyPointConstants.URL_ST_PROPERTY);
			 
			JsonElement element = jsonObject.get("value");
			jsonObject =element.getAsJsonObject();
			element = jsonObject.get("items");
			JsonArray arry  = element.getAsJsonArray();
			
			Type listType = new TypeToken<List<Property>>() {}.getType();
			
			propertyList = new Gson().fromJson(arry.toString(), listType);
//			return PropertyFeedUtil.getInstance().processSTPropertyFeed(parameterMap, PropertyPointConstants.URL_ST_PROPERTY);
		} catch (Exception e) {
			
			e.printStackTrace();
			throw new FeedErrorException(e.getMessage());
		}
		
		return propertyList;
	}

	
	private String[] getLocations(String[] locations) {
		String location = null;
		if (locations != null && locations[0] != null) {
			location = locations[0].toUpperCase();
		} else {
			return new String[] {};
		}

		if (D01.toUpperCase().contains(location)) {
			return new String[] { "D01" };
		} else if (D02.toUpperCase().contains(location)) {
			return new String[] { "D02" };
		} else if (D03.toUpperCase().contains(location)) {
			return new String[] { "D03" };
		} else if (D04.toUpperCase().contains(location)) {
			return new String[] { "D04" };
		} else if (D05.toUpperCase().contains(location)) {
			return new String[] { "D05" };
		} else if (D06.toUpperCase().contains(location)) {
			return new String[] { "D06" };
		} else if (D07.toUpperCase().contains(location)) {
			return new String[] { "D07" };
		} else if (D08.toUpperCase().contains(location)) {
			return new String[] { "D08" };
		} else if (D09.toUpperCase().contains(location)) {
			return new String[] { "D09" };
		} else if (D10.toUpperCase().contains(location)) {
			return new String[] { "D10" };
		} else if (D11.toUpperCase().contains(location)) {
			return new String[] { "D11" };
		} else if (D12.toUpperCase().contains(location)) {
			return new String[] { "D12" };
		} else if (D13.toUpperCase().contains(location)) {
			return new String[] { "D13" };
		} else if (D14.toUpperCase().contains(location)) {
			return new String[] { "D14" };
		} else if (D15.toUpperCase().contains(location)) {
			return new String[] { "D15" };
		} else if (D16.toUpperCase().contains(location)) {
			return new String[] { "D16" };
		} else if (D17.toUpperCase().contains(location)) {
			return new String[] { "D17" };
		} else if (D18.toUpperCase().contains(location)) {
			return new String[] { "D18" };
		} else if (D19.toUpperCase().contains(location)) {
			return new String[] { "D19" };
		} else if (D20.toUpperCase().contains(location)) {
			return new String[] { "D20" };
		} else if (D21.toUpperCase().contains(location)) {
			return new String[] { "D21" };
		} else if (D22.toUpperCase().contains(location)) {
			return new String[] { "D22" };
		} else if (D23.toUpperCase().contains(location)) {
			return new String[] { "D23" };
		} else if (D24.toUpperCase().contains(location)) {
			return new String[] { "D24" };
		} else if (D25.toUpperCase().contains(location)) {
			return new String[] { "D25" };
		} else if (D26.toUpperCase().contains(location)) {
			return new String[] { "D26" };
		} else if (D27.toUpperCase().contains(location)) {
			return new String[] { "D27" };
		} else {
			return new String[] { "D28" };
		}

	}
	

}
